import {
	mapState,
	mapMutations
} from "vuex"
import {
	getShoppingCartList,
	userLogin,
	updateProductNum
} from "./api.js"
export default {
	data() {
		return {
			isEdit: false,
			cartList: [],
			selectSet: new Set(),
			recommendList: [],
			 totalPrice: 0.0,
			 totalNumber: 0 
		}
	},
	onShow() {
		this.onShowCall();
	},
	onLoad(options) {

	},
	computed: {
		
	},
	methods: {
		getTotalPrice: function() {
			let p = 0.0;
			this.cartList.forEach((element, index, array) => {
				if(this.selectSet.has(element.id)){
					p = p + element.amount;
				}
			});
			this.totalPrice = p;
		},
		getTotalNum: function() {
			let n = 0;
			this.cartList.forEach((element, index, array) => {
				if(this.selectSet.has(element.id)){
					n = n + element.number;
				}
			});
			this.totalNumber = n;
		},
		openEdit() {
			this.isEdit = true;
		},
		closeEdit() {
			this.isEdit = false;
		},
		deleteCart() {

		},
		//   vuex储存信息
		...mapMutations([
			"setCartCount",
			"setShopInfo", //设置店铺信息
			"setShopPhone", //设置电话
			"setShopStatus", //设置店铺状态
			"initdishListMut", //设置购物车订单
			"setStoreInfo",
			"setBaseUserInfo", //设置用户基本信息
			"setLodding",
			"setToken", //设置token
			"setDeliveryFee", //设置配送费
		]),
		// 从vuex信息
		...mapState([
			"cartCount",
			"shopInfo", //店铺信息
			"shopPhone", //电话
			"orderListData",
			"baseUserInfo", //用户信息
			"lodding",
			"token", //token
			"deliveryFee", //配送费
		]),
		unChecked(item) {
			// console.log(item)
			if (this.selectSet.has(item.id)) {
				this.selectSet.delete(item.id);
				this.totalPrice = this.totalPrice - item.amount;
				this.totalNumber = this.totalNumber - 1;
			} else {
				this.selectSet.add(item.id);
				this.totalPrice = this.totalPrice + item.amount;
				this.totalNumber = this.totalNumber + 1;
			}
			this.$forceUpdate(); // 强制 Vue 更新 DOM
		},
		onShowCall() {
			getShoppingCartList().then((res) => {
				this.cartList = res.data
				for (let item of this.cartList) {
					this.selectSet.add(item.id);
				}
				this.getTotalPrice();
				this.getTotalNum();
			}).catch((err) => {})
		},
		onShowCallNotSelectList() {
			getShoppingCartList().then((res) => {
				this.cartList = res.data
				this.getTotalPrice();
				this.getTotalNum();
			}).catch((err) => {})
		},
		addProductToCart(item) {
			let updateItem = {
				...item
			};;
			updateItem.number += 1;
			this.callUpdateProductNum(updateItem);
			this.setCartCount(this.cartCount() + 1)
		},
		minusProductToCart(item) {
			let updateItem = {
				...item
			};;
			updateItem.number -= 1;
			if (updateItem.number === 0) {
				return;
			} else {
				this.callUpdateProductNum(updateItem);
				this.setCartCount(this.cartCount() - 1)
			}
		},
		callUpdateProductNum(item) {
			updateProductNum(item).then((res) => {
				if (res.code == 200) {
					this.onShowCallNotSelectList();
					
				}
			}).catch((err) => {
				uni.showToast({
					title: '操作失败',
					icon: "fail",
					duration: 1000,
				});
			})
		},
		// 去订单页面
		goOrder() {
			uni.navigateTo({
				url: "/pages/order/index",
			})
		},
		// 删除商品
		deleteItem(index) {
			// this.goodsList.splice(index, 1);

		}
	},

}